<template>
  <div class="dashboard-container">
    <el-card shadow="never">
      <div class="header">
        <span style="font-size: 12px; color: pink">说明：目前支持学科和关键字条件筛选</span>
        <el-button
          type="success"
          size="small"
          icon="el-icon-edit"
          @click="$router.push('new-l')"
          >新增试题</el-button
        >
      </div>
      <!-- 表单 -->
      <el-form :model="query" label-width="120px" >
        <el-row>
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select
                v-model="query.subjectID"
                placeholder="请选择"
                @change="onSubjectChange"
                size="small"
              >
                <el-option
                  v-for="item in subjectsList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select
                v-model="query.catalogID"
                @change="onDirectoryChange"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  v-for="(item, index) in directory"
                  :key="index"
                  :label="item.directoryName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select
                v-model="query.tags"
                placeholder="请选择"
                @change="onDirectoryChange"
                size="small"
              >
                <el-option
                  v-for="item in tagsList"
                  :key="item.value"
                  :label="item.tagName"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item  label="关键字">
              <el-input v-model="query.keyword" placeholder="根据题干搜索" size="small"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select
                v-model="query.questionType"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  v-for="item in questionType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select
                v-model="query.difficulty"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  v-for="item in difficulty"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select
                v-model="query.direction"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  v-for="(item, index) in direction"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select
                v-model="query.creatorID"
                placeholder="请选择"
                size="small"
              >
                <el-option
                  v-for="(item, index) in creatorList"
                  :key="index"
                  :label="item.username"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item  label="题目备注">
              <el-input v-model="query.remarks" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="query.shortName" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-select
                placeholder="请选择"
                class="city"
                size="small"
                v-model="query.province"
                @change="onProvinceChange"
              >
                <el-option
                  v-for="(item, index) in provinces"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
              <el-select
                placeholder="请选择"
                class="area"
                size="small"
                v-model="query.city"
              >
                <el-option
                  v-for="(item, index) in citys"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="margin-left: 80px">
              <el-button @click="clearAll" size="small">清除</el-button>
              <el-button @click="getList" size="small" type="primary">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-alert
        :title="'数据一共' + counts + '条'"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>

      <!-- end -->
      <!-- 数据 -->
      <el-table
        :key="tableKey"
        :data="dataList"
        v-loading="listLoading"
        element-loading-text="给我一点时间"
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column align="center" label="试题编号">
          <template slot-scope="scope">
            <span>{{ scope.row.number }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="学科">
          <template slot-scope="scope">
            <span>{{ scope.row.subject }}</span>
          </template>
        </el-table-column>
        <el-table-column label="目录">
          <template slot-scope="scope">
            <span>{{ scope.row.catalog }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="题型">
          <template slot-scope="scope">
            <span v-html="formatterQuestionType(scope.row.questionType)"></span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="题干">
          <template slot-scope="scope">
            <span v-html="html2Text(scope.row.question)"></span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="录入时间">
          <template slot-scope="scope">
            <span v-html="formatTime(scope.row.addDate)"></span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="难度">
          <template slot-scope="scope">
            <span v-html="formatterDifficulty(scope.row.difficulty)"></span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="录入人">
          <template slot-scope="scope">
            <span>{{ scope.row.creator }}</span>
          </template>
        </el-table-column>
        <!-- 按钮 -->
        <el-table-column
          width="200px"
          align="center"
          label="操作"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              plain
              @click="preview(scope.row)"
              title="预览"
              icon="el-icon-view"
              circle
            ></el-button>
            <el-button
              type="success"
              size="small"
              plain
              title="修改"
              @click="toEdit(scope.row)"
              icon="el-icon-search"
              circle
            ></el-button>
            <el-button
              type="danger"
              size="small"
              plain
              @click="removeUser(scope.row.id)"
              title="删除"
              icon="el-icon-delete"
              circle
            ></el-button>
            <el-button
              type="warning"
              size="small"
              plain
              title="加入精选"
              @click="addSelection(scope.row.id)"
              icon="el-icon-check"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- end -->

      <!-- 分页 -->
      <el-pagination
        style="text-align: right; margin-top: 20px; margin-bottom: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="query.pagesize"
        layout="prev, pager, next, sizes, jumper"
        background
        :total="counts"
      >
      </el-pagination>

      <!-- 试题预览 -->
      <QuestionsPreview :dialogVisible="dialogVisible" @close="dialogVisible = false" :questionItem="questionItem" />
    </el-card>
  </div>
</template>

<script>
import {
  list,
  del,
  change,
  subjectList,
  tagsList,
  creatorsList,
  detail,
  direcotrys
} from '@/api/hmmm/questions'
import QuestionsPreview from './../components/questions-preview.vue'
import { difficulty, questionType, direction } from '@/api/hmmm/constants'
import { provinces, citys } from '@/api/hmmm/citys'
import { parseTime, html2Text } from '@/utils/index'
export default {
  name: 'Questions',
  components: {
    QuestionsPreview
  },
  data () {
    return {
      // 预览时传入子组件的试题
      questionItem: {},
      // 试题预览
      dialogVisible: false,
      // 学科简单列表
      subjectsList: [],
      // 标签简单列表
      tagsList: [],
      // 录入人列表
      creatorList: [],
      // 二级目录列表
      directory: [],
      // 题型
      questionType,
      // 难度
      difficulty,
      // 方向
      direction,
      // 区域列表
      citys,
      // 城市列表
      provinces: provinces(),
      // 数据总数
      counts: 0,
      // 题库列表
      dataList: [],
      tableKey: 0,
      total: null,
      listLoading: true,
      // 查询信息
      query: {
        page: 1, // 当前页数
        pagesize: 5, // 每页显示多少
        subjectID: null, // 学科
        difficulty: null, // 难度
        questionType: null, // 试题类型
        tags: null, // 标签名称
        province: null, // 企业所在地省份
        city: null, // 企业所在城市
        keyword: null, // 关键字
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        direction: null, // 方向
        creatorID: null, // 录入人
        catalogID: null // 目录
      }
    }
  },
  computed: {},
  methods: {
    // 获取列表数据
    async getList () {
      this.listLoading = true
      const { data } = await list(this.query)

      this.dataList = data.items
      this.counts = data.counts
      this.listLoading = false
    },
    // 格式化题型
    formatterQuestionType (row, column, cellValue, index) {
      // row 当前行的对象
      // column 列属性
      // cellValue 当前标签渲染的内容
      // index是对象的索引号
      // 要去找 1所对应的值
      // find方法 只要查到第一个满足条件的 就返回这个item 循环结束
      // 全部循环完毕查不到 返回undefined
      const obj = questionType.find((item) => item.value === +row)
      return obj ? obj.label : '未知'
    },
    // 格式化难度
    formatterDifficulty (row, column, cellValue, index) {
      // row 当前行的对象
      // column 列属性
      // cellValue 当前标签渲染的内容
      // index是对象的索引号
      // 要去找 1所对应的值
      // find方法 只要查到第一个满足条件的 就返回这个item 循环结束
      // 全部循环完毕查不到 返回undefined
      const obj = difficulty.find((item) => item.value === +row)
      return obj ? obj.label : '未知'
    },
    // 格式化时间
    formatTime (row, column, cellValue, index) {
      return parseTime(row)
    },
    // 格式化文字
    html2Text (row, column, cellValue, index) {
      return html2Text(row)
    },
    // 加入精选
    async addSelection (id) {
      await this.$confirm('此操作将该题目加入精选 ' + ', 是否继续?', '提示', {
        type: 'warning'
      })
      await change(id, 1)
      this.$message.success('加入精选成功')
      // 重新拉取页面
      this.getList()
    },
    // 删除
    async removeUser (id) {
      // 提示是否删除
      await this.$confirm('此操作将永久删除用户 ' + ', 是否继续?', '提示', {
        type: 'warning'
      })
      // 发删除的请求
      await del(id)
      // 刷新页面
      this.getList()
      // 提示删除删除成功
      this.$message.success('删除成功！')
    },
    // 获取简单的学科列表
    async getSubjectList () {
      const { data } = await subjectList()
      this.subjectsList = data
    },
    // 获取简单的标签列表
    async getTagsList () {
      const { data } = await tagsList({
        subjectID: this.query.subjectID
      })
      this.tagsList = data.items
    },
    // 获取录入人列表
    async getCreatorList () {
      const { data } = await creatorsList({
        page: 1,
        pagesize: 100,
        keyword: ''
      })
      this.creatorList = data.list
    },
    // 清除表单内容
    clearAll () {
      for (const key in this.query) {
        if (key === 'page' || key === 'pagesize') {
          continue
        }
        this.query[key] = null
      }
      this.tagList = this.directory = []
      // 更新题库列表
      this.getList()
    },
    // 当页码发生变化
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getList()
    },
    // 当前页发生变化
    handleCurrentChange (val) {
      this.query.page = val
      this.getList()
    },
    // 当市级发生变化时触发的函数
    onProvinceChange (val) {
      this.query.city = ''
      this.citys = citys(val)
    },
    // 目录选择框发生变化后触发的函数
    onDirectoryChange (val) {
      // 获取标签
      this.getTagsList()
    },
    // 预览
    async preview (item) {
      // 显示对话框
      const { data } = await detail({ id: item.id })
      data.questionType = item.questionType
      data.subject = item.subject
      this.dialogVisible = true
      // 传入试题
      this.questionItem = data
    },
    //  学科选择框发生变化后触发的函数
    async onSubjectChange (val) {
      this.query.tags = null
      this.query.catalogID = null
      const { data } = await direcotrys({ subjectID: val })
      console.log(data)
      this.directory = data.items
      this.tagsList = []
    },
    // 去修改试题的页面
    toEdit (row) {
      this.$router.push({
        path: 'new-l',
        query: {
          id: row.id
        }
      })
    }
  },
  // 创建完毕状态
  created () {
    // 读取数据
    this.getList()
    // 获取简单的学科列表
    this.getSubjectList()
    // 获取简单的标签列表
    // this.getTagsList()
    // 获取录入人列表
    this.getCreatorList()
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
  // text-align: right;
}
</style>

<style>
.header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
.el-table th {
  background-color: #fafafa;
}
.el-table th.is-leaf {
  border-bottom: 2px solid #e8e8e8;
}
/* .el-form-item {
  margin-bottom: 0;
} */
.disabled td {
  background-color: #f9f9f9;
  color: #c1c1c1;
}
.disabled .el-button--primary,
.disabled .el-button--danger {
  background-color: #dbdada;
  border: 1px solid #dbdada;
  color: #999;
  cursor: not-allowed;
}
.city {
  width: 45%;
  margin-right: 5%;
}
.area {
  width: 50%;
}
</style>
